/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.core.domain.model.LoginUser;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.satoken.utils.LoginHelper;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.domain.bo.query.IsfdRegionLibraryQueryBo;
import com.comac.ins.isfd.domain.vo.IsfdRegionLibraryMongoVo;
import com.comac.ins.isfd.service.IIsfdRegionLibraryMongoService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;

/**
 * 【铺层库蒙皮属性(mongo)】
 *
 * @author wujialin
 * @date 2025-06-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/mongo/regionLibrary")
public class IsfdRegionLibraryMongoController extends BaseController {

    private final IIsfdRegionLibraryMongoService isfdRegionLibraryMongoService;

    /**
     * 查询【区域库】列表
     */
    @SaCheckPermission("isfd:regionLibrary:list")
    @PostMapping("/list")
    public TableDataInfo<IsfdRegionLibraryMongoVo> list(@RequestBody IsfdRegionLibraryQueryBo queryBo) {
        return isfdRegionLibraryMongoService.queryPageList(queryBo);
    }

    /**
     * 导出【区域库】列表
     */
    @SaCheckPermission("isfd:regionLibrary:export")
    @Log(title = "【区域库】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@RequestBody IsfdRegionLibraryQueryBo queryBo, HttpServletResponse response) {
        isfdRegionLibraryMongoService.export(queryBo, response);
    }

    /**
     * 导出【区域库】模板
     */
    @SaCheckPermission("isfd:regionLibrary:export")
    @Log(title = "【区域库】模板", businessType = BusinessType.EXPORT)
    @GetMapping("/template")
    public void downloadTemplate(HttpServletResponse response) {
        isfdRegionLibraryMongoService.exportTemplate(response);
    }


    /**
     * 导入【区域库】数据
     */
    @SaCheckPermission("isfd:regionLibrary:import")
    @Log(title = "【区域库】", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public R<Long> importRegionLibrary(@RequestParam("file") MultipartFile file) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        File tempFile = null;
        try {
            // MultipartFile 转为临时 File 对象
            tempFile = File.createTempFile("regionLibraryImport_", ".xlsx");
            file.transferTo(tempFile);

            // 调用导入服务
            Long successCount = isfdRegionLibraryMongoService.excelImport(tempFile, loginUser);
            return R.ok(successCount);
        } catch (Exception e) {
            return R.fail("导入失败：" + e.getMessage());
        } finally {
            if (tempFile != null && tempFile.exists()) {
                tempFile.delete();
            }
        }
    }


    /**
     * 获取【区域库】详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("isfd:regionLibrary:query")
    @GetMapping("/{id}")
    public R<IsfdRegionLibraryMongoVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable String id) {
        return R.ok(isfdRegionLibraryMongoService.queryById(id));
    }

    /**
     * 保存/修改【区域库】
     */
    @SaCheckPermission("isfd:regionLibrary:save")
    @Log(title = "【区域库】", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public R<Void> save(@RequestBody IsfdRegionLibraryMongoVo vo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        return toAjax(isfdRegionLibraryMongoService.save(vo, loginUser));
    }

    /**
     * 删除【区域库】
     *
     * @param ids 主键串
     */
    @SaCheckPermission("isfd:regionLibrary:remove")
    @Log(title = "【区域库】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] ids) {
        return toAjax(isfdRegionLibraryMongoService.deleteWithValidByIds(List.of(ids), true));
    }
}
